#include <kern/monitor.h>
#include <kern/vm.h>
#include <kern/multiboot.h>
#include <kern/proc.h>
#include <kern/trap.h>
#include <kern/debug.h>
#include <kern/sched.h>

static void create_init_proc(void)
{
	extern uint8_t _binary_obj_user_init_start[];
	extern uint8_t _binary_obj_user_init_size[];

	struct proc_t* initproc = NULL;

	if (proc_alloc(&initproc, 0))
		panic("unable to alloc init proc");

	proc_load_code(initproc, _binary_obj_user_init_start,
		(int)_binary_obj_user_init_size);

	if (sched_set_runnable(initproc))
		panic("unable to make init runnable");

}

void kmain(unsigned int magic)
{
	monitor_clear();
	monitor_write("Welcome to SaturnOS ! satring kernel ...\n");


	vm_init();

	proc_init();
	sched_init();
	trap_init();

	create_init_proc();

	// TODO: init multicore
	// from now on, all function are invoked by all cpus
	vm_init_gdt();
	trap_init_idt();

	scheduler();
}

